/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.editors;

import faneclipse.Activator;
import faneclipse.preferences.FanPreferenceConstants;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * This class has all the attributes for the syntax highlighting.
 * @author BEGAUDEAU stephane
 */
public class FanTextAttributeProvider {
	/**
	 * The attribute for the comments.
	 */
	public static final String COMMENT_ATTRIBUTE = "__fan_comment_attribute";
	/**
	 * The atribute for the default text.
	 */
	public static final String DEFAULT_ATTRIBUTE = "__fan_default_attribute";
	/**
	 * the attribute for the keyword of the language.
	 */
	public static final String KEYWORD_ATTRIBUTE = "__fan_keyword_attribute";
	/**
	 * The attribute for the strings.
	 */
	public static final String STRING_ATTRIBUTE = "__fan_string_attribute";
	
	/**
	 * Color of comments.
	 */
	private static RGB commentsColor = PreferenceConverter.getColor(Activator.getDefault().getPreferenceStore(), FanPreferenceConstants.COMMENTS_PREFERENCE);
	
	/**
	 * Color of keywords.
	 */
	private static RGB keywordsColor = PreferenceConverter.getColor(Activator.getDefault().getPreferenceStore(), FanPreferenceConstants.KEYWORDS_PREFERENCE);
	
	/**
	 * Color by default.
	 */
	private static RGB defaultColor = PreferenceConverter.getColor(Activator.getDefault().getPreferenceStore(), FanPreferenceConstants.DEFAULT_PREFERENCE);
	
	/**
	 * Color of strings.
	 */
	private static RGB stringsColor = PreferenceConverter.getColor(Activator.getDefault().getPreferenceStore(), FanPreferenceConstants.STRINGS_PREFERENCE);
	
	/**
	 * A map of the attributes and their name.
	 */
	private Map<String, TextAttribute> fAttributes = new HashMap<String, TextAttribute>();
	
	/**
	 * The constructor.
	 */
	public FanTextAttributeProvider() {
		fAttributes.put(COMMENT_ATTRIBUTE, new TextAttribute(new Color(Display.getCurrent(), commentsColor)));
		fAttributes.put(KEYWORD_ATTRIBUTE, new TextAttribute(new Color(Display.getCurrent(), keywordsColor)));
		fAttributes.put(DEFAULT_ATTRIBUTE, new TextAttribute(new Color(Display.getCurrent(), defaultColor)));
		fAttributes.put(STRING_ATTRIBUTE, new TextAttribute(new Color(Display.getCurrent(), stringsColor)));
	}
	
	/**
	 * @param type The name of the desired attribute or the default attribute if we can't find one.
	 * @return The attribute which matches the given name.
	 */
	public final TextAttribute getAttribute(final String type) {
		TextAttribute attr = (TextAttribute) fAttributes.get(type);
		if(attr == null) {
			attr = (TextAttribute) fAttributes.get(DEFAULT_ATTRIBUTE);
		}
		return attr;
	}

	/**
	 * @return The color of the comments.
	 */
	public static final RGB getCommentsColor() {
		return commentsColor;
	}

	/**
	 * @param commentsColor The new color of the comments.
	 */
	public static final void setCommentsColor(final RGB commentsColor) {
		FanTextAttributeProvider.commentsColor = commentsColor;
	}

	/**
	 * @return The color of the keywords.
	 */
	public static final RGB getKeywordsColor() {
		return keywordsColor;
	}

	/**
	 * @param keywordsColor the new color of the keywords.
	 */
	public static final void setKeywordsColor(final RGB keywordsColor) {
		FanTextAttributeProvider.keywordsColor = keywordsColor;
	}

	/**
	 * @return The color by default.
	 */
	public static final RGB getDefaultColor() {
		return defaultColor;
	}

	/**
	 * @param defaultColor The new color by default.
	 */
	public static final void setDefaultColor(final RGB defaultColor) {
		FanTextAttributeProvider.defaultColor = defaultColor;
	}

	/**
	 * @return The color of the strings.
	 */
	public static final RGB getStringsColor() {
		return stringsColor;
	}

	/**
	 * @param stringsColor The new color of the strings.
	 */
	public static final void setStringsColor(final RGB stringsColor) {
		FanTextAttributeProvider.stringsColor = stringsColor;
	}
}
